package logic

import (
	"beego_background_framework/models"
	"beego_background_framework/utils"
	"github.com/astaxie/beego/context"
	"path"
	"strconv"
)

//主要逻辑类

//组装权限节点数据为树形结构
func MakeRuleTree(adminAuthRule []*models.AdminAuthRule,parentId uint) ([]models.TreeMenu) {
	var treeMenu []models.TreeMenu
	var tempTreeMenu models.TreeMenu

	for _,v := range adminAuthRule{
		if v.Pid == parentId{
			tempTreeMenu.AdminAuthRule = v
			//找到子菜单
			for _,vv := range adminAuthRule{
				if v.Id == vv.Pid{
					tempTreeMenu.Children = append(tempTreeMenu.Children,vv)
				}
			}
			treeMenu = append(treeMenu, tempTreeMenu)
			//置空对象
			tempTreeMenu.AdminAuthRule = nil
			tempTreeMenu.Children = nil
		}
	}
	return treeMenu
}

//自动创建日期文件夹,路径相对于static下
func AutoCreateUploadDateDir(path string) string{
	allPath := "static/upload/" + path
	return utils.CreateDateDir(allPath)
}

//验证后缀名是否符合要求
func CheckFileExt(fileName string) bool {
	var AllowExtMap = map[string]bool{
		".jpg":true,
		".jpeg":true,
		".png":true,
	}
	ext := path.Ext(fileName)
	if _,ok:=AllowExtMap[ext];!ok{
		return false
	}
	return true
}

//获取完整路径
func GetFullPath(ctx *context.Context,filePath string) string {
	var allNewFilePath string
	port := ctx.Input.Port()
	if port == 80{
		allNewFilePath = ctx.Input.Site() + "/" +filePath
	}else{
		allNewFilePath = ctx.Input.Site() + ":"+ strconv.Itoa(port) + "/" +filePath
	}
	return allNewFilePath
}
